<?php
namespace Model;

use Core\Config;
use Core\Constant;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Predicate\Like;
use Zend\Db\Sql\Predicate\Operator;
use Zend\Db\Sql\Select;

class PrivilegeTable extends AbstractTable
{

    /**
     * @param $id
     * @return Privilege
     */
    public function getPrivilege($id){
       return $this->getById($id);
    }

    /**
     * @param $name
     * @return Privilege
     */
    public function getPrivilegeByName($name)
    {
       return $this->getByColumn('name', $name)->current();
    }

    public function searchAllPrivilege($isPaging, $offset, $rowPerPage, $orderArr = array('name'), $searchOption){

        $select = $this->getDbSqlSelect();

        if($searchOption['name']){
            $select->where(array('name like "%'.$searchOption['name'].'%"'));

        }

        if($searchOption['privilegeTypeId']){
            $select->where(array('type_id ='.$searchOption['privilegeTypeId']));
        }

        if($isPaging){
            $select->offset($offset)->limit($rowPerPage);
        }

        if($orderArr){
            $select->order($orderArr);
        }

        $rows = $this->tableGateway->selectWith($select);
        $result['rows'] = $rows;

        $total = $this->getCount($select);

        $result['total'] = $total;

        return $result;

    }


    public function getAllPrivilege()
    {
        $select = $this->getDbSqlSelect();
        $select->join(array('pt'=>'privilege_type'),
            'pt.id = privilege.type_id', array("type_name"=>"name"),$select::JOIN_LEFT);
        $select->order('type_id desc');
        return $this->getTableGateway()->selectWith($select);
    }


    public function getAllAuthenticatedPagePrivilege()
    {
        $select = $this->getDbSqlSelect();
        $select->join("page", "page.privilege_id = privilege.id", array("page"=>"page.name"))
            ->where('page.is_publish_for_all = 0');
        return $this->getTableGateway()->selectWith($select);
    }

    public function getAllFrontActionPrivilege()
    {
        $select = $this->getDbSqlSelect();
        $select->join("authorize_action", "authorize_action.privilege_id = privilege.id",
            array("module"=>"authorize_action.module", "controller"=>"authorize_action.controller",
                "action"=>"authorize_action.action"))
            ->where(array('authorize_action.module'=> Config::getFrontModule()))
        ->order("controller");
        return $this->getTableGateway()->selectWith($select);
    }

    public function getAllAdminActionPrivilege()
    {
        $select = $this->getDbSqlSelect();
        $select->join("authorize_action", "authorize_action.privilege_id = privilege.id",
            array("module"=>"authorize_action.module", "controller"=>"authorize_action.controller",
                "action"=>"authorize_action.action"))
            ->where(array('authorize_action.module'=> Config::getAdminModule()))
            ->order("controller");
        return $this->getTableGateway()->selectWith($select);
    }

    public function getAllPagePrivilege()
    {
        $select = $this->getDbSqlSelect();
        $select->join(array('pt'=>'privilege_type'), 'pt.id = privilege.type_id', array())
            ->where(array('pt.code'=>Constant::$PRIVILEGE_TYPE_ACCESS_PAGE_CODE));

        return $this->getTableGateway()->selectWith($select);
    }

    public function getAllActionPrivilege()
    {
        $select = $this->getDbSqlSelect();
        $select->join(array('pt'=>'privilege_type'), 'pt.id = privilege.type_id', array())
            ->where(array('pt.code'=>Constant::$PRIVILEGE_TYPE_ACCESS_ACTION_CODE));
        return $this->getTableGateway()->selectWith($select);
    }
}